﻿using Core.SimpleTemp.Application.ServiceApp.SysApp.FileApp;
using Core.SimpleTemp.Application.UserApp;
using Core.SimpleTemp.Common;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;

namespace Core.SimpleTemp.Mvc.Controllers.Sys
{
    /// <summary>
    /// 文件上传
    /// </summary>
    [Route("api/[controller]")]
    [EnableCors("any")]
    public class FileController : CoreApiController
    {
        private readonly SysFileAppService _service;
        private WebAppOptions _webAppOptions;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="service"></param>
        /// <param name="webAppOptions"></param>
        public FileController(SysFileAppService service, IOptionsMonitor<WebAppOptions> webAppOptions)
        {
            _service = service;
            _webAppOptions = webAppOptions.CurrentValue;
        }


        /// <summary>
        /// 附件上传培训接口
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        [HttpPost("UploadFile")]
        public async Task<IActionResult> UploadFileAsync([FromForm]UpdateFileDto files)
        {
           

            var errorActionResult = CheckFile(files);
            if (errorActionResult != null)
                return errorActionResult;

            var uploadFileDtoList = await _service.UploadFileAsync(files);
            if (uploadFileDtoList != null && uploadFileDtoList.Any())
            {
                uploadFileDtoList.ForEach(a => a.FilePath = string.Empty);//隐藏物理路径
                return this.JsonSuccess(uploadFileDtoList);
            }
            else
                return this.JsonFaild();
        }


        /// <summary>
        /// 附件上传通用接口
        /// </summary>
        /// <param name="files"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        [HttpPost("UploadFileForPXPlan")]
        public async Task<IActionResult> UploadFileForPXPlan([FromForm]UpdateFileDto files, [FromForm]String data)
        {

            if (data == null)
                return this.JsonFaild("培训计划ID为空");

            var errorActionResult = CheckFile(files);
            if (errorActionResult != null)
                return errorActionResult;

            var uploadFileDtoList = await _service.UploadFileForPXPlan(files,data);
            if (uploadFileDtoList != null && uploadFileDtoList.Any())
            {
                uploadFileDtoList.ForEach(a => a.FilePath = string.Empty);//隐藏物理路径
                return this.JsonSuccess(uploadFileDtoList);
            }
            else
                return this.JsonFaild();
        }



        /// <summary>
        /// 校验附件信息
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        private IActionResult CheckFile(UpdateFileDto files)
        {
            if (files == null || files.Files == null)
                return this.JsonFaild("至少上传一个文件");
            else
                return null;
        }

        /// <summary>
        /// ppt文件上传并生成在线浏览文件
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        [HttpPost("UploadPPT")]
        public async Task<IActionResult> UploadPPTAsync([FromForm]UpdateFileDto files)
        {
            var errorActionResult = CheckFile(files);
            if (errorActionResult != null)
                return errorActionResult;
            foreach (var item in files?.Files)
            {
                var ext = System.IO.Path.GetExtension(item.FileName);
                if (ext != ".ppt" && ext != ".pptx")
                    return this.JsonFaild("文件类型不正确");
            }
            var uploadFileDtoList = await _service.UploadPPTAsync(files);
            if (uploadFileDtoList != null && uploadFileDtoList.Any())
            {
                //uploadFileDtoList.ForEach(a => a.FilePath = string.Empty);//隐藏物理路径
                return this.JsonSuccess(uploadFileDtoList);
            }
            else
                return this.JsonFaild();
        }



        /// <summary>
        /// 下载附件
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{DownLoad}")]
        public async Task<IActionResult> DownLoad(Guid id)
        {
            var fileModel = await _service.GetAsync(id);
            if (fileModel == null)
                throw new CustomOperationException("未找到对应文件");
            var stream = System.IO.File.OpenRead(_service.CreatePath(fileModel.FilePath));
            string fileExt = Path.GetExtension(fileModel.FileName);
            //获取文件的ContentType
            var provider = new FileExtensionContentTypeProvider();
            var memi = provider.Mappings[fileExt];
            return File(stream, memi, Path.GetFileName(fileModel.FileName));
        }

    }
}
